// 使用Reflect的defineMetadata与getMetadata配置验证数据
import 'reflect-metadata'
// 参数装饰器
const RequireDecorator: ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => {
  // 哪些参数需要验证
  let requiredParams: number[] = []
  requiredParams.push(parameterIndex)

  Reflect.defineMetadata('required', requiredParams, target, propertyKey)
  // console.log('RequireDecorator');

}

// 方法装饰器
const validateDecorator: MethodDecorator = (target: Object, propertyKey: string | symbol, descriptor: PropertyDescriptor) => {
  const method = descriptor.value
  // console.log(Reflect.getMetadata('required', target, propertyKey));
  descriptor.value = function () {
    const requiredParams: number[] = Reflect.getMetadata('required', target, propertyKey) || []

    const args = arguments
    requiredParams.forEach(index => {
      if (index > args.length || args[index] == undefined) {
        throw new Error('请传入必要的参数')
      }
    })
    method.apply(this, args)
  }
  // console.log('validateDecorator');

}

class Info {
  @validateDecorator
  find(name: string, @RequireDecorator id: number) {
    console.log(id);

  }
}

// new Info().find('12', undefined)
new Info().find('12', 1)
